package com.hjc.springbootmovies.controller;

import com.hjc.springbootmovies.entity.ModelUsers;
import com.hjc.springbootmovies.service.Impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by IntelliJ IDEA.
 * User: HJC
 * Date: 2019/12/4
 * Time: 14:34
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/users")
public class UsersInfoController {
    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("usersInfo")
    public String login(String uid, String psw, Model model , HttpServletRequest request){
        ModelUsers modelUsers = userService.queryById(uid,psw);
        if(modelUsers != null){
            request.getSession().setAttribute("users" , modelUsers);
            return "html/main/main";
        }else{
            model.addAttribute("ErrMsg","is *error uid or password !");
            return "html/index";
        }
    }

    @GetMapping("logout")
    public String logout( Model model , HttpServletRequest request){
        HttpSession  session = request.getSession();
        session.setAttribute("users",null);
        model.addAttribute("ErrMsg","over logout.");
        return "html/index";
    }
}
